/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.com;

final public class UserInfoDataStruct {

    private int id; // default is 0 (zero) and it can't be lower 1
    private String username;
    private String name;
    private byte[] encryptedPW;
    private String position;
    private String email;
    private String roles;
  
    private static final char[] DEFAULT_PASSWORD = {'9', '9', '9'};

    public static final int USERNAME_INDEX = 0;
    public static final int NAME_INDEX = 1;
    public static final int EPASSWORD_INDEX = 2;
    public static final int POSITION_INDEX = 3;
    public static final int EMAIL_INDEX = 4;
    public static final int ROLES_INDEX = 5;
  
    public UserInfoDataStruct() {
        this.id = 0;
        this.username = "";
        this.name = "";
        this.encryptedPW = WADTools.encryptPassword(DEFAULT_PASSWORD);
        this.position = "";
        this.email = "";
        this.roles = "";
    }
  
  public UserInfoDataStruct(String username) {
      this.id = 0;
      this.username = username;
      this.name = "";
      this.encryptedPW = WADTools.encryptPassword(DEFAULT_PASSWORD);
      this.position = "";
      this.email = "";
      this.roles = "";
  }
  
  public UserInfoDataStruct(int id, String username, String name, String position, String email, String roles) {
      this.id = id;
      this.username = username;
      this.name = name;
      this.position = position;
      this.email = email;
      this.roles = roles;
  }

  public UserInfoDataStruct(int id, String username, byte[] encryptedPW, String name, String position, String email, String roles) {
      this.id = id;
      this.username = username;
      this.encryptedPW = encryptedPW;
      this.name = name;
      this.position = position;
      this.email = email;
      this.roles = roles;
  }
  
  public void setUsername(String un) {
    username = un;
  }
  
  public String getUsername() {
    return username;
  }
  
  public void setName(String n) {
    name = n;
  }
  
  public String getName() {
    return name;
  }
  
  public void setEncryptedPassword(byte[] epw) {
    encryptedPW = epw;
  }
  
  public byte[] getEncryptedPassword() {
    return encryptedPW;
  }
  
  public void setPosition(String p) {
    position = p;
  }
  
  public String getPosition() {
    return position;
  }
  
  public void setEMail(String em) {
    email = em;
  }
  
  public String getEMail() {
    return email;
  }
  
  public Object getAttributeAt(int index) {
    switch(index){
      case USERNAME_INDEX:
        return username;
      case NAME_INDEX:
        return name;
      case POSITION_INDEX:
        return position;
      case EMAIL_INDEX:
        return email;
      case ROLES_INDEX:
        return roles;
      default:
        return null;
    }
  }
  
  public boolean isPasswordMatch(byte[] ipw) {
    if(ipw.length != encryptedPW.length)
      return false;
    
    for(int i=0;i<ipw.length;i++){
      System.out.println(encryptedPW[i]+" "+ipw[i]);
      if(encryptedPW[i] != ipw[i])
        return false;
    }
    
    return true;
  }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the roles
     */
    public String getRoles() {
        return roles;
    }

    /**
     * @param roles the roles to set
     */
    public void setRoles(String roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        String text = id+"\t"+username+"\t"+name+"\t"+position+"\t"+email+"\t"+roles;
        return text;
    }
}
